package com.abc.scholarship.service.impl;

import com.abc.scholarship.common.enums.StatusEnum;
import com.abc.scholarship.domain.entity.Clazz;
import com.abc.scholarship.exception.BusinessException;
import com.abc.scholarship.mapper.ClassMapper;
import com.abc.scholarship.service.ClassService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author jzw
 * @since 2024-07-02
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Clazz> implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public List<Clazz> getNotExitByNames(List<String> names) {
        Set<String> collect = classMapper.selectList(new LambdaQueryWrapper<Clazz>()
                .select(Clazz::getClassName)
                .in(Clazz::getClassName, names)
        ).stream().map(Clazz::getClassName).collect(Collectors.toSet());

        return new ArrayList<>(
                names.stream()
                        .filter(item -> !collect.contains(item))
                        .map(Clazz::new)
                        .collect(Collectors.toSet())
        );
    }

    @Override
    public void setStatusByIds(List<Long> idList, Integer status) {
        classMapper.setStatusByIds(idList, status);
    }

    @Override
    public void MUpdateById(Clazz clazz) {
        if (clazz.getStatus() == null) {
            classMapper.updateById(clazz);
            return;
        }
        if (clazz.getStatus().equals(StatusEnum.FROZEN.getValue())) {
            clazz.setStatus(StatusEnum.NORMAL.getValue());
        } else if (clazz.getStatus().equals(StatusEnum.NORMAL.getValue())) {
            clazz.setStatus(StatusEnum.FROZEN.getValue());
        } else throw new BusinessException("状态异常，请联系管理员");
        classMapper.updateById(clazz);
    }
}
